package games.graphPath;

import java.util.List;

import commonDataStructures.BinaryHeap;

public class GraphPathAStarSearch{
	
	private Graph _graph;

	private int _maxDepth;

	
	public GraphPathAStarSearch(Graph g, int maxDepth){
		_graph = g;
		_maxDepth = maxDepth;
	}
	
	public void search(GraphPathResult r){
				
		//ArrayList<GraphPathAStarState> path = new ArrayList<GraphPathAStarState>();
		//path.add(_curState);
						
		long beginTime = System.currentTimeMillis();

		int depth = -1;
		GraphPathAStarState curState = new GraphPathAStarState(_graph,null,0,0,0,_graph.getSrc());
		BinaryHeap<GraphPathAStarState> moves = new BinaryHeap<GraphPathAStarState>();
		moves.insert(curState);
		List <GraphPathAStarState> temp;
		while (!curState.isWin() && depth<=_maxDepth){
			depth++;
			curState = moves.remove();
			temp = curState.getPossibleMoves();
			for (GraphPathAStarState s : temp)
				moves.insert(s);
			
			if (moves.isEmpty())
				break;
			
			
			//path.add(_curState);
			
			/*
			GraphPathAStarState best = (GraphPathAStarState)moves.get(0);
			for (int i=1; i<moves.size(); i++){
				if (best.compareTo((GraphPathAStarState)moves.get(i))<0){
					best = (GraphPathAStarState)moves.get(i);
				}
			}
			_curState = best;
			*/
		}
		long totalTime = System.currentTimeMillis()-beginTime;

		if (!curState.isWin()){
			r.addFailed(GraphPathResult.A_STAR);
		}
		
		r.add(GraphPathResult.A_STAR,curState,depth,totalTime);

	}
	
	

}
